function getCatLog(novelname, catlogname) {
		$.post("/wfRead/novel/loadbookcatlog",
				"bookname=" + novelname, function (data) {
						var list = data.data;
						$("#catlogList").empty();
						var positioncatlog = 0;
						for (var i = 0; i < list.length; i++) {
								var li = "<li id=\"li" + i + "\"><a href=\"" + "/wfRead/read/" + novelname + "/" + list[i].novelCatalog.toString() + "\">" + list[i].novelCatalog.toString() + "</a></li>";
								$("#catlogList").append(li);
								var str = list[i].novelCatalog.toString();
								if (catlogname == str) {
										positioncatlog = i;
										if (i != 0) {
												$(".lastCatlog")[0].href = "/wfRead/read/" + novelname + "/" + list[i - 1].novelCatalog.toString();
												$(".lastCatlog")[1].href = "/wfRead/read/" + novelname + "/" + list[i - 1].novelCatalog.toString();
										}

										$(".novelCatlog")[0].href = "/wfRead/novel/book/" + novelname;
										$(".novelCatlog")[1].href = "/wfRead/novel/book/" + novelname;

										if (i != list.length - 1) {
												$(".nextCatlog")[0].href = "/wfRead/read/" + novelname + "/" + list[i + 1].novelCatalog.toString();
												$(".nextCatlog")[1].href = "/wfRead/read/" + novelname + "/" + list[i + 1].novelCatalog.toString();
										}
										catlogname = "false";
								}
						}
						$("#catlogList").scrollTop(positioncatlog * 34);
						$("#li" + positioncatlog).css("background", "#CCE8CF");
				}, "json");
}

function getText(novelname, catlogname) {
		var context = $("#content");

		$(".bookname h1").text(catlogname);
		$(".menu_title").text(catlogname);
		$("#nav_novel").text(novelname);
		$("#nav_novel")[0].href = "/wfRead/novel/book/" + novelname;
		$.post("/wfRead/novel/readbycatlog",
				"novelname=" + novelname + "&catlogname=" + catlogname,
				function (data) {
						context.empty();
						var text = data.data;
						context.text(text);
						if (data.flag) {

								$("#content").css("height", $("#content").css("height"));
						} else {
								/*<div class="vip-limit-wrap" style="display: block">
												<h3 class="lang"><i>这是VIP章节</i>需要订阅后才能阅读</h3>
										<div class="subscribe-btn-wrap dib-wrap no-subscribe-btn-box" id="vip-btn-a">
												<a class="all j_subscribeBtn " data-soa="1" href="javascript:" data-eid="qd_R112"><em>全部订阅</em><span id="all-amount">1117书币</span></a>
												<a class="single j_subscribeBtn" data-soa="0" data-uid="132" href="javascript:" data-eid="qd_R113" style="right: 155px;"><em>订阅本章</em><span id="this-amount">15书币</span></a>
										</div>
								  </div>*/
								context.append('<div class="vip-limit-wrap" style="display: block">\n' +
										'<h3 class="lang"><i>这是VIP章节</i>需要订阅后才能阅读</h3>\n' +
										'<div class="subscribe-btn-wrap dib-wrap no-subscribe-btn-box" id="vip-btn-a">\n' +
										'\t\t<a class="all j_subscribeBtn " onclick="buyAll()" data-soa="1"  data-eid="qd_R112"><em>全部订阅</em><span id="all-amount">' + data.message.split(":")[1] + '书币</span></a>\n' +
										'\t\t<a class="single j_subscribeBtn" data-soa="0" onclick="buyOne()" data-uid="132"  data-eid="qd_R113" style="right: 155px;">' +
										'<em>订阅本章</em><span id="this-amount">' + data.message.split(":")[0] + '书币</span></a>\n' +
										'</div>')
						}
				}, "json")
}

function light() {
		var value = $("#night").val();
		if (value == 0) {
				$("div").attr("style", "background-color:rgb(17, 17, 17); color: rgb(147, 147, 146)");
				font_ex();
				fontsize_ex()
				width_ex();
				color_ex();
				$("#night").val(1);
		} else if (value == 1) {
				var bgc = $("#bgcselect").val();
				$("div").removeAttr("style", "background-color:rgb(17, 17, 17); color: rgb(147, 147, 146)");
				bgc_ex();
				font_ex();
				fontsize_ex()
				width_ex();
				color_ex();
				$("#night").val(0);
		}
		saveConfig();
}

function bgc_ex() {
		var bgc = $("#bgcselect").val();
		if (bgc == "默认" || bgc == "背景") {
				bgc = "#E9FAFF";
		}
		$(".content_read").css("background-color", bgc);
		saveConfig();
}

function width_ex() {
		var width = $("#bcolor").val();
		if (width == "默认" || width == "宽度") {
				width = "95%";
		}
		$("#content").width(width);
		saveConfig();
}

function font_ex() {
		var font = $("#fontselect").val();
		if (font == "字体" || font == "默认") {
				font = "宋体";
		}

		$("#content").css("font-family", font);
		saveConfig();
}

function fontsize_ex() {
		var font = $("#fontsizeselect").val();
		if (font == "默认" || font == "大小") {
				font = "19pt";
		}
		$("#content").css("font-size", font);
		saveConfig();
}

function color_ex() {
		var color = $("#colorselect").val();
		if (color == "默认" || color == "颜色") {
				color = "#000";
		}

		$("#content").css("color", color);
		saveConfig();
}

function voteTicket() {

		//1、点击发送请求
		$.post("/wfRead//novel/voteticket",
				"bookname=" + $("#nav_novel").text(), function (data) {
						if (!data.flag) {
								alert(data.message);
						} else {
								alert("感谢你的票！");
						}

				}, "json");
}
/*
* 入口
* */
$(function () {
		$(".recommend").on("click", voteTicket);
		$(".bookmark").on("click", addBookMark);
		$("#close-panel").on("click", closeBuyBox);
		$(".red-btn").on("click", payForOne)
		$(".chapter_forum_btn").on("click", openCommentBox)
		$(".QUI_POP_CLOSE").on("click", closeCommentBox);
		$(".Btn").on("click",sendComment)
		// $(".blue-btn").on("click",recharge)
		loadData();
		readConfig();
		getPage(1, 10);
})

//单章订阅
function payForOne() {
		var novelName = $("#nav_novel").text();
		var catalogName = $(".menu_title").text();
		var status = $("#pay-status").val();
		if (status == 1) {
				//alert(novelName+"  :  "+catalogName);
				$.post("/wfRead/reader/payforone",
						"novelName=" + novelName + "&catalogName=" + catalogName,
						function (data) {
								if (data.flag) {
										alert(data.message);
										window.location.reload()
								} else {
										alert(data.message);
								}
						}, "json")
		} else if (status == 999) {
				$.post("/wfRead/reader/payforall",
						"novelName=" + novelName,
						function (data) {
								if (data.flag) {
										alert(data.message);
										window.location.reload()
								} else {
										alert(data.message);
								}
						}, "json")
		}

}

//重写getPage（）方法
function getPage(currentPage, pageSize) {
		var url = window.location.href;
		url = "/wfRead" + decodeURIComponent(url.split("wfRead")[1]);
		var novelName = $("#nav_novel").text()
		//请求后台，获取评论
		$.post("/wfRead/novel/getcomments",
				"currentPage=" + currentPage + "&pageSize=" + pageSize + "&novelName=" + novelName + "&url=" + url,
				function (data) {
						if (data.flag) {
								var info = data.data;
								console.log(info)
								if (info == null||info.size==0) {
										console.log("erro=" + data.message);
										$("#JthreadList").css("display", "none");
										$("#no_comments").css("display", "block");
										$("#erro_msg").text(data.message);
								} else {
										$("#JthreadList").css("display", "block");
										$("#no_comments").css("display", "none");
										var pageInfo = info;
										var size = pageInfo.size < 10 ? pageInfo.size : 10;
										var list = pageInfo.list;
										$(".chapter_morethread .current_page").val(pageInfo.pageNum);
										$(".chapter_morethread .all_page").text(pageInfo.pages);
										$("#JthreadList").empty();
										var pref = (pageInfo.pageNum - 1) * 10;
										for (var i = 0; i <size ; i++) {
												console.log(list[i])
												var li = '<li >\n' +
														'\t\t\t\t<div class="user-head"><a href="" target="_blank">' +
														'<img src="/wfRead/user-photo/'+list[i].commentAuthor.nickName+'/'+list[i].commentAuthor.headImage+'" alt="" width="50" height="50"><em class="lv2"></em> </a>\n' +
														'\t\t\t</div>\n' +
														'\t\t\t<div class="for-rp-con">\n' +
														'\t\t\t\t\t<div class="name">'+list[i].commentAuthor.nickName+'<span id="vip_leave">'+list[i].commentAuthor.vip+'</span></div>\n' +
														'\t\t\t\t\t<div class="dec clearfix hide JdecAll "><a class="aLabel" href=""\n' +
														'\t\t\t\t\t\t\ttarget="_blank">'+list[i].commentContext+'</a></div>\n' +
														'\t\t\t\t\t<div class="other">\n' +
														'\t\t\t\t\t\t\t<div class="date fl">'+ Format(list[i].commentTime, "yyyy-MM-dd HH:mm") +'</div>\n' +
														'\t\t\t\t\t</div>\n' +
														'\t\t\t</div>\n' +
														'</li>';
												$("#JthreadList").append(li);
										}
								}
						} else {
								console.log(data.message);
								$("#JthreadList").css("display", "none");
								$("#no_comments").css("display", "block");
								$("#erro_msg").text(data.message);
						}
				}, "json")

}

function buyOne() {
		openBuyBox();
		var price = $("#this-amount").text();
		price = price.replace("书币", "");
		$(".current-price").empty().text(price)
		$(".diff").empty().text(price);
		$("#pay-status").val(1);
}

function buyAll() {
		openBuyBox();
		var price = $("#all-amount").text().replace("书币", "");
		$(".current-price").empty().text(price)
		$(".diff").empty().text(price);
		$("#pay-status").val(999);
}

function openBuyBox() {
		$(".lbf-panel").css("display", "block");
		$.post("/wfRead/reader/getbalance", "", function (data) {
				//console.log(data.data);
				if (data.flag) {
						$(".current-balance").empty().text(data.data);
				} else {
						$(".current-balance").empty().text(0);
				}
		}, "json")
}

function closeBuyBox() {
		$(".lbf-panel").css("display", "none");
}

/*
* 加载小说章节内容
* */
function loadData() {
		var url = window.location.href;
		var paths = url.split("/");
		var novelname = decodeURIComponent(paths[paths.length - 2]);
		var catlogname = decodeURIComponent(paths[paths.length - 1]);
		//加载小说目录
		getCatLog(novelname, catlogname);
		//加载章节
		getText(novelname, catlogname);
		$('.recommend').href = "blank";
}

/*
* 添加书签
* */
function addBookMark() {
		var url = window.location.href;
		var paths = url.split("/");
		var book_name = decodeURIComponent(paths[paths.length - 2]);
		var catlogname = decodeURIComponent(paths[paths.length - 1]);

		$.post("/wfRead/reader/addbookmark",
				"book_name=" + book_name + "&catlogname=" + catlogname,
				function (data) {
						alert(data.message);
				}, "json")

}

/*
* 保存页面配置到cookie中
* */
function saveConfig() {
		var url = decodeURIComponent(window.location.href);
		path = url.split("/")[url.split("/").length - 1]
		url = url.split(path)[0]
		//console.log(url)
		//$.cookie('light',$("#night").val(), {path:url});
		document.cookie = 'night=' + $("#night").val() + '; path=' + url;
		//$("#bgcselect").val()
		document.cookie = 'bgcselect=' + $("#bgcselect").val() + '; path=' + url;
		//$("#bcolor").val()
		document.cookie = 'bcolor=' + $("#bcolor").val() + '; path=' + url;
		//$("#fontselect").val()
		document.cookie = 'fontselect=' + $("#fontselect").val() + '; path=' + url;
		//$("#fontsizeselect").val()
		document.cookie = 'fontsizeselect=' + $("#fontsizeselect").val() + '; path=' + url;
		//$("#colorselect").val()
		document.cookie = 'colorselect=' + $("#colorselect").val() + '; path=' + url;
}

/*
* 从cookie获取页面配置
* */
function readConfig() {
		var cookies = document.cookie;
		//console.log(cookies.split(";"));

		if (cookies==""){
				console.log(cookies)
				return;
		}

		cookies = cookies.split(";");
		var flag = false;

		for (var i = 0; i < cookies.length; i++) {
				let kv = cookies[i].split("=");
				let key = kv[0].replace(" ", "");
				let value = kv[1];

				if (key == "night" && value != $('#' + key).val()) {
						value = $("#" + key).val()
						flag = true;
				}
				var selector ='#' + key;
				console.log(selector)
				$(selector).val(value);
		}
		font_ex();
		fontsize_ex();
		color_ex();
		width_ex();
		bgc_ex();
		if (flag) {
				light();
		}
}

function openCommentBox() {
		$(".QUI_POP_BOX").css("display", "block");
}

function closeCommentBox() {
		$(".QUI_POP_BOX").css("display", "none");
}

/*
* 发表评论
* */
function sendComment() {

		var context = $(".POP_Topic textarea").val();
		var url = window.location.href;
		var paths = url.split("/")
		var novelName = decodeURIComponent(paths[paths.length - 2]);
		var catalog = decodeURIComponent(paths[paths.length - 1]);
		url = "/wfRead" + decodeURIComponent(url.split("wfRead")[1]);
		if (context == "") {
				alert("评论不能为空！");
		}
		if (catalog == "") {
				alert("页面错误，请刷新重试！");
		}
		if (novelName == "") {
				alert("页面错误，请刷新重试！");
		}
		$.post("/wfRead/reader/addcomment",
				"context="+context+"&url="+url+"&novelName="+novelName+"&catalog="+catalog,
				function (data) {
						if (!data.flag){
								alert(data.message)
						}else {
								closeCommentBox();
								$(".POP_Topic textarea").val("");
								getPage(1,10);
						}
				},"json")
}